<?php

function getSQL()
{
    global $bdd;
    if(!$_SESSION["login"]){
        $req = $bdd->query("SELECT * FROM notes WHERE public = 1 ORDER BY ID ASC") or die(print_r($bdd->errorInfo()));
    }
    else {
        $req = $bdd->prepare("SELECT * FROM notes WHERE user = ? OR public = 1 ORDER BY ID ASC");
        $req->execute(array($_SESSION["ID"])) or die(print_r($req->errorInfo()));
    }
    $result = $req->fetchAll();
    $req->closeCursor();
    return $result;
}

function getItem($id)
{
    global $bdd;
    $req = $bdd->prepare("SELECT * FROM notes WHERE ID = :id AND (user = :user OR public = 1) ORDER BY ID ASC");
    $req->execute(array('id' => $id, 'user' => $_SESSION["ID"])) or die(print_r($req->errorInfo()));
    $result = $req->fetchAll();
    $req->closeCursor();
    return $result;
}

function goAjoutNotes($name, $website, $infos, $user, $public){
    global $bdd;

    $req = $bdd->prepare('INSERT INTO notes(name, website, infos, user, public) VALUES (:name, :website, :infos, :user, :public)');
    $req->execute(array('name' => $name, 'website' => $website, 'infos' => $infos, 'user' => $user, 'public' => $public)) or die(print_r($req->errorInfo()));
    $req->closeCursor();
    return '';
}

function goEditNotes($id, $name, $website, $infos, $public){
    global $bdd;
    
    $req = $bdd->prepare('UPDATE notes SET name = :name, website = :website, infos = :infos, public = :public WHERE ID = :id');
    $req->execute(array('name' => $name, 'website' => $website, 'infos' => $infos, 'public' => $public,
        'id' => $id)) or die(print_r($req->errorInfo()));
    $req->closeCursor();
    return '';
}

function goDeleteNotes($id){
    global $bdd;
    $req = $bdd->prepare('DELETE FROM notes WHERE ID=?');
    $req->execute(array($id)) or die(print_r($req->errorInfo()));
    $req->closeCursor();
    return '';
}